package com.lpf.hotelsystem.service.impl;

import com.lpf.hotelsystem.mapper.LoginMapper;
import com.lpf.hotelsystem.pojo.Admin;
import com.lpf.hotelsystem.pojo.User;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImp {
    @Resource
    private LoginMapper loginMapper;

    public Map<String, Object> login( String username,  String password) {
        Admin adminVO = new Admin();// 把前端传递的值对象数据，自动封装到pojo对象中去(反射机制)
        adminVO.setUsername(username);
        adminVO.setUserpass(password);
        Admin admin = loginMapper.login(adminVO);
        /*
        1.user对象如果为null  代表用户名没有找到
        2.user对象不为null  但是密码对不上
        3.user对象不为null  并且密码也对上
         */
        Map<String, Object> map = new HashMap<>();
        if(admin == null) {
            map.put("code", 60001);
            map.put("msg", "对不起，用户名不存在！");
        }
        else {
            String sql_psd = admin.getUserpass();
            String user_psd = DigestUtils.md5DigestAsHex(adminVO.getUserpass().getBytes(StandardCharsets.UTF_8));
            System.out.println("sql_psd ===== " + user_psd);
            if(sql_psd.equals(user_psd)) {
                map.put("code", 200);
                map.put("msg", "登录成功！");
            }
            else {
                map.put("code", 60002);
                map.put("msg", "密码错误！");
            }
        }

        return map;
    }
}
